package net.tv90.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.tv90.auth.dao.AccountMapper;
import net.tv90.auth.dao.UserMapper;
import net.tv90.auth.model.IdentifyInfo;
import net.tv90.auth.model.domain.Account;
import net.tv90.auth.model.domain.User;
import net.tv90.auth.service.SendMailService;
import net.tv90.auth.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    AccountMapper accountMapper;

    @Autowired
    JavaMailSender mailSender;

    @Autowired
    JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    SendMailService mailService;

    @Transactional
    @JmsListener(destination = "queue.identify")
    public void sendCodeMailListener(IdentifyInfo identifyInfo) {
        try {
            Account account = accountMapper.selectByUsername(identifyInfo.getUsername());
            if(account != null) {
                updateCodeAndRole(
                        identifyInfo.getUsername(),
                        identifyInfo.getCode(),
                        identifyInfo.getRole()
                );
                mailService.sendIdentifyMailTo(account.getEmail(), account.getUsername());
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public User queryUserByAccount(String username) {
        QueryWrapper<Account> accountQuery = new QueryWrapper<>();
        accountQuery.eq("username", username);
        Account account = accountMapper.selectOne(accountQuery);
        if (account == null) {
            return null;
        }
        QueryWrapper<User> userQuery = new QueryWrapper<>();
        userQuery.eq("account_id", account.getId());
        return userMapper.selectOne(userQuery);
    }

    public void updateCodeAndRole(String username, String code, Integer role) {
        User user = queryUserByAccount(username);
        user.setCode(code);
        user.setRole(role);
        user.setLastUpdateTime(LocalDateTime.now());
        userMapper.updateById(user);
    }

    /**
     * 查询已认证的用户的学号/工号
     * @param username
     * @return 返回学号或工号
     */
    @Override
    public String getCode(String username) {
        QueryWrapper<Account> accountQuery = new QueryWrapper<>();
        accountQuery.eq("username", username);
        Account account = accountMapper.selectOne(accountQuery);
        if(account != null) {
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("account_id", account.getId());
            User user = userMapper.selectOne(userQueryWrapper);
            return user == null ? null : user.getCode();
        }
        return null;
    }
}
